package com.lanou3g.musicenjoy.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import com.bartoszlipinski.flippablestackview.FlippableStackView;
import com.bartoszlipinski.flippablestackview.OrientedViewPager;
import com.bartoszlipinski.flippablestackview.StackPageTransformer;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.FlippableStackViewAdapter;


import com.lanou3g.musicenjoy.entity.FlippableStackViewEntity;
import com.lanou3g.musicenjoy.volley.VolleySingleton;
import android.util.Log;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by 李宁 on 15/10/27.
 * 翻页效果页面
 */
public class FlippableStackViewFragment extends Fragment {
    private FlippableStackView stack;
    private FlippableStackViewAdapter mStackAdapter;
    private StringRequest stringRequest;
    private String startUrl = "http://api.dongting.com/frontpage/frontpage?uid=0&f=f0&v=v8.2.0.2015091720&utdid=VJU5DHOX3d4DAE1KKtjt%2BPXO&net=2&s=s200&location=1";
    private List<FlippableStackViewEntity.DataEntity> fsv;
    private RequestQueue requestQueue;
    private ProgressDialog dialog;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_flippablestackview, null);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestQueue = VolleySingleton.getInstance(getActivity()).getRequestQueue();
        stack = (FlippableStackView) getActivity().findViewById(R.id.stack);

        getUrl();
    }
    public void getUrl(){
        stringRequest = new StringRequest(startUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                FlippableStackViewEntity result = gson.fromJson(response, FlippableStackViewEntity.class);
                Log.i("xinixn", result + " 00000");
                 if (result != null){
                    fsv = result.getData();
                    stack.initStack(fsv.size(), StackPageTransformer.Orientation.VERTICAL);//设置页卡数和方向
                     Log.i("xinixn", fsv + " 00000");
//                    mStackAdapter.setData(fsv);
                     mStackAdapter = new FlippableStackViewAdapter(getActivity(), fsv);
                    stack.setAdapter(mStackAdapter);
                     Log.i("xinixn", mStackAdapter.getCount() + " 00000");

                }
                if (dialog.isShowing()){
                    dialog.dismiss();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        requestQueue.add(stringRequest);
        //加载中
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("请稍等,乐享音乐加载中");
        dialog.show();
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
